﻿using DFGK.WMS.Common.JWT;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI;
using DFGK_WMS.WebAPI.Controllers.Result;
using MathNet.Numerics.Distributions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserRepository userRepository;
        private readonly ILogRepository LogService;
        private readonly IMemoryCache cache;

        public UserController(IUserRepository userRepository,ILogRepository LogService,IMemoryCache cache, TokenHelper tokenHelper)
        {
            this.userRepository = userRepository;
            this.LogService = LogService;
            this.cache = cache;
            TokenHelper = tokenHelper;
        }

        public TokenHelper TokenHelper { get; }
        /// <summary>
        /// 判断登录
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public IActionResult GetLogin(LoginPDADTO m)
        {
            if (string.IsNullOrEmpty(m.User_Name) || m.User_Name.Length == 0)
            {
                return Ok(BaseResult.Result("400", "账号不能为空！"));
            }
            if (string.IsNullOrEmpty(m.User_Password) || m.User_Password.Length == 0)
            {
                return Ok(BaseResult.Result("400", "密码不能为空！"));
            }
            //var captcha = cache.Get("captcha" + HttpContext?.Connection?.RemoteIpAddress?.ToString())?.ToString();
            //if (captcha is null)
            //    return Ok(BaseResult.Result("400", "还未请求验证码"));
            //if (captcha != m.ImgCaptcha)
            //    return Ok(BaseResult.Result("400", "验证码不正确"));
            if (string.IsNullOrEmpty(m.User_Name) || string.IsNullOrEmpty(m.User_Password))
            {
                return Ok(BaseResult.Result("400", "账号密码不能为空"));
            }
            ApiResult apiResult = new ApiResult();
            var obj = userRepository.GetLoginPDA(m);
            if (obj != null)
            {
                string? ip = HttpContext?.Connection?.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 0;
                logg.ActionType = 1;
                logg.ModuleName = "系统登录";
                logg.UserName = obj.UserName;
                logg.RealName = obj.RealName;
                logg.UserId = obj.Id;
                logg.Description = "IP地址为" + ip + "用户名为" + m.User_Name + "，登录成功！";
                //获取前端IP地址
                logg.Ipaddress = ip;

                LogService.AddTLod(logg);
                //返回一个空的密码
                obj.Pwd = string.Empty;
                var token = TokenHelper.CreateJwtToken(obj);
                HttpContext.Response.Headers["token"] = token;
                Response.Headers["Access-Control-Expose-Headers"] = "token";
                obj.Token = token;
                return Ok(BaseResult.Result(obj, "登录成功！", "200"));
            }
            apiResult.code = "500";
            apiResult.message = "账号密码有误！";
            return Ok(apiResult);
        }
    }
}
